//go:build !oracle
// +build !oracle

package sql

import _ "gitee.com/micro-plat/qtask/internal/modules/const/sql/mysql"

const SQLGetSEQ = `insert into tsk_system_seq (name,create_time) values (@name, now())`

const SQLQueryTaskForInsert = `
select t.task_id from tsk_system_task t 
where t.order_no = @order_no
and t.queue_name = @queue_name
and t.name = @name
and t.plat_name = @plat_name
&t.ext_no
and t.status in(20,30)
limit 1
`

const SQLCreateTask = `insert into tsk_system_task
(task_id,
 name,
 plat_name,
 next_execute_time,
 max_execute_time,
 next_interval,
 delete_interval,
 status,
 max_count,
 order_no,
 ext_no,
 queue_name,
 msg_content)
values
(@task_id,
 @name,
 @plat_name,
 date_add(now(),interval #first_timeout second),   
 date_add(now(),interval #max_timeout second),
 @next_interval,
 @delete_interval,
 20,
 @max_count,
 @order_no,
 @ext_no,
 @queue_name,
 @msg_content)`

const SQLProcessingTask = `
update tsk_system_task t set 
t.last_update_time = now(),
t.next_execute_time=date_add(now(),interval t.next_interval second),
t.status=30,
t.count=t.count + 1,
t.last_execute_time=now()
where t.task_id=@task_id 
and t.status in(20,30)`

const SQLFinishTask = `
update tsk_system_task t set 
    t.next_execute_time = STR_TO_DATE('2099-12-31', '%Y-%m-%d'),
    t.status            = 0,
    t.last_update_time = now(),
    t.delete_time       = date_add(now(),interval t.delete_interval second)
where t.task_id = @task_id
and t.status in (20, 30)`

const SQLUpdateTask = `
update tsk_system_task t set 
t.batch_id=@batch_id,
t.last_update_time = now(),
t.next_execute_time = date_add(now(),interval t.next_interval second)
where t.max_execute_time > now() 
and t.next_execute_time <= now() 
and (t.count < t.max_count or t.max_count=0)
and t.plat_name = @plat_name
and t.status in(20,30)
ORDER BY RAND()
limit 200`

const SQLQueryWaitProcess = `
select t.queue_name,t.msg_content 
from tsk_system_task t
where t.batch_id=@batch_id 
and t.next_execute_time > now()`

const SQLClearTask = `delete from tsk_system_task where delete_time < now() and status in (0, 90)`

const SQLFailedTask = `
UPDATE tsk_system_task t SET 
t.last_update_time = now(),
t.delete_time = DATE_ADD(NOW(),INTERVAL CASE WHEN t.delete_interval=0 THEN 604800 ELSE t.delete_interval END SECOND),
t.status = 90
WHERE ((t.max_execute_time > DATE_SUB(NOW(),INTERVAL 7 DAY)
AND t.max_execute_time < DATE_SUB(NOW(),INTERVAL 1 HOUR)) OR (t.count >= t.max_count and t.max_count!=0))
AND t.status IN (20, 30)
limit 5000
`

const SQLClearSEQ = `delete from tsk_system_seq where create_time < date_add(now(),interval -600 second)`

const SQLClosetask = `
update tsk_system_task t set 
t.last_update_time = now(),
    t.next_execute_time = STR_TO_DATE('2099-12-31', '%Y-%m-%d'),
    t.status            = 91,
    t.delete_time       = date_add(now(),interval t.delete_interval second)
where t.task_id = @task_id
AND t.status IN (20, 30)
`

// SQLUpdateTaskByTaskID 更新任务通过id
const SQLUpdateTaskByTaskID = `
UPDATE tsk_system_task t SET 
 t.last_update_time = now(),
 t.next_execute_time =  DATE_ADD(NOW(), INTERVAL if(@first_timeout='',0,#first_timeout) second),
 t.max_execute_time = IF(@max_timeout != '', DATE_ADD(NOW(), INTERVAL if(@max_timeout='',0,#max_timeout) second),t.max_execute_time),
 t.next_interval = IF(@next_interval='',t.next_interval,@next_interval),
 t.max_count = IF(@max_count='',t.max_count, t.count + @max_count),
 t.msg_content = IF(@msg_content='',t.msg_content, @msg_content)
WHERE t.task_id = @task_id
AND t.status IN (20, 30)
`
const SQLTaskRepeat = `
UPDATE tsk_system_task t SET 
t.last_update_time = now(),
t.next_execute_time = date_add(now(),interval if(@first_timeout='',0,#first_timeout) second),   
t.msg_content=if(@msg_content='',t.msg_content,@msg_content),
t.max_execute_time= DATE_ADD(NOW(), INTERVAL #max_timeout second) 
where t.task_id=@task_id
AND t.status IN (20, 30)
`

const SQLQueryTaskByID = `
select count(1) from tsk_system_task t where t.task_id = @task_id
`
